package com.metaverse.backend.config;

import com.anji.captcha.util.StringUtils;
import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    //本地存储
    @Value("${storage.local_path:}")
    private String localPath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        // registry.addResourceHandler("webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        if(StringUtils.isNotBlank(localPath)){
            registry.addResourceHandler("/files/**").addResourceLocations("file:" + localPath);
        }

    }


    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.stream().filter(converter -> converter instanceof MappingJackson2HttpMessageConverter)
                .forEach(converter -> {
                    ObjectMapper objectMapper = ((MappingJackson2HttpMessageConverter) converter).getObjectMapper();
                    SimpleModule simpleModule = new SimpleModule();
                    simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
                    simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
                    objectMapper.registerModule(simpleModule);
                    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                    SerializationConfig serializationConfig = objectMapper.getSerializationConfig().withView(Views.OtherView.class);
                    objectMapper.setConfig(serializationConfig);
                    ((MappingJackson2HttpMessageConverter) converter).setObjectMapper(objectMapper);
                });
        System.out.println(converters);
    }
    // @Bean
    // public MappingJackson2HttpMessageConverter getMappingJackson2HttpMessageConverter() {
    //     MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
    //     //设置日期格式
    //     ObjectMapper objectMapper = new ObjectMapper();
    //     objectMapper.setDateFormat(CustomDateFormat.instance);
    //     objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    //     mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
    //     //设置中文编码格式
    //     List<MediaType> list = new ArrayList<>();
    //     list.add(MediaType.APPLICATION_JSON_UTF8);
    //     mappingJackson2HttpMessageConverter.setSupportedMediaTypes(list);
    //     return mappingJackson2HttpMessageConverter;
    // }

//    @Override
//    public void addFormatters(FormatterRegistry registry) {
//        DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
//        registrar.setUseIsoFormat(true);
//        registrar.registerFormatters(registry);
//    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedHeaders("*")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("HEAD", "GET", "PUT", "POST", "DELETE", "PATCH")
                .exposedHeaders("Content-Disposition");
    }

}
